在前幾篇之中,我們簡介了k3s + Deployment的相關方案,相信各位應該都有練習了(測試有問題跟我們說啊!),在Pod派送之中,我們前幾天都只對後端服務進行存取與副本機制的取用,而如何設定該Pod所需要的硬體資源,並為Pod設計指定的派送節點,在kubernetes之中有提及,其使用Node Selector、Node Affinity(Hard/Soft)等方案,而在k3s之中,印為他的架構follow在kubernetes之上,所以我們也同樣來進行一下測(折)試(疼),並測試Node label節點標註功能是否可以運行在k3s之上,了解一下載節點選擇的方案之上,k3s與kubernetes是否養相異之處。
Node Selector 方案為直接選擇 Node Label的功能(暴力解法),直接尋找叢集內部的所有節點,查詢是否出現所需要的節點標籤,這邊會先教學一下如何建立節點標籤,並嘗試使用Node Selector方案協助Pod派送到指定的節點之上。
k3s kubectl get node -o wide
k3s kubectl label node ${node-name} ${key=value}
k3s kubectl get node --show-labels
apiVersion: v1
kind: Pod
metadata:
name: nginx-node-selector
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
nodeSelector:
network: 100m
k3s kubectl create -f ${node-affinity-file}
k3s kubectl get pod -o wide
k3s kubectl describe pod -o wide
apiVersion: v1
kind: Pod
metadata:
name: nginx-node-selector
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
nodeSelector:
network: 1000m
k3s kubectl create -f ${node-affinity-file}
k3s kubectl get pod -o wide
k3s kubectl describe pod -o wide
Hard Mode 以必要性節點為出發,對於特定硬體資源的節點標籤為規劃(如:需要Nvidia的顯卡),當Pod內服務只支援某一特定硬體環境時使用,而當Node無法達成該項條件時則會暫停Pod的派送,以較為強制需求性的硬體資源為標籤參考方案,因此稱為Hard Mode。
k3s kubectl get node -o wide
k3s kubectl label node ${node-name} ${key=value}
k3s kubectl get node --show-labels
apiVersion: v1
kind: Pod
metadata:
name: grafana-node-hdd
labels:
app: grafana
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: grafana
image: grafana/grafana
k3s kubectl create -f ${node-affinity-file}
k3s kubectl get pod -o wide
k3s kubectl describe pod
apiVersion: v1
kind: Pod
metadata:
name: grafana-node-hdd
labels:
app: grafana
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- sshd
containers:
- name: grafana
image: grafana/grafana
k3s kubectl create -f ${node-affinity-file}
k3s kubectl get pod -o wide
k3s kubectl describe pod
Soft Mode 是以參考相關Node Label,通常是以非必要硬體基礎節點標籤為規劃,或是以不影響功能的節點標籤為基準,並以加權(加分)方式進行節點選擇,其不同於Hard Mode直接阻擋派送,而是當節點標籤不存在或是不匹配時,將Pod派送到其他可用的節點之上。
k3s kubectl get node -o wide
k3s kubectl label node ${node-name} ${key=value}
k3s kubectl get node --show-labels
apiVersion: v1
kind: Pod
metadata:
name: grafana-node-12.04
labels:
app: grafana
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: os.version
operator: In
values:
- "12.04"
containers:
- name: grafana
image: grafana/grafana
k3s kubectl create -f ${node-affinity-file}
k3s kubectl get pod -o wide
k3s kubectl describe pod
apiVersion: v1
kind: Pod
metadata:
name: grafana-node-12.04
labels:
app: grafana
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: os.version
operator: In
values:
- "16.04"
containers:
- name: grafana
image: grafana/grafana
k3s kubectl create -f ${node-affinity-file}
k3s kubectl get pod -o wide
k3s kubectl describe pod
本篇介紹了Node Affinity的相關功能,下篇我們將介紹Pod Affinity的功能。